package
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	import flash.events.TimerEvent;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.utils.Timer;
	
	import flashx.textLayout.formats.TextAlign;
	
	import smlab.utils.PopUpUtils;
	import smlab.view.ui.list.List_SM;
	
	[SWF(width=800,height=600)]
	/**
	 * 抽奖匀减速
	 * @author Simon
	 * 
	 */
	public class SlowDown extends Sprite
	{
		private var list:List_SM;
		private var totalTime:uint=6000;//ms
		private var delay:uint=100;//ms
		private var currentDelay:uint=delay;//ms
		private var t:Timer=new Timer(delay);
		private var t1:TextField;
		private var t2:TextField;
		private var t3:TextField;
		private var t4:TextField;
		private var isTickMode:Boolean=false;//是否已进入缓动模式
		
		private var currentBling:uint=0;//当前闪的格
		private var totalGiftNum:uint;//总可抽数量，从1开始
		private var giftIndex:uint;//抽中奖品的是第几格，从0开始
		private var nextRunGap:int;//点停止后总共还需要跑多少格子,从1开始
		private var nextRunGapNoChange:uint;//点停止后总共还需要跑多少格子,从1开始
		private var leftTime:uint;//最后跑的总格子，除了正常时间，多出多少时间
		private var x1:uint=0;//平均加速度的倍数
		private var x2:uint;//平均加速度
		
		public function SlowDown()
		{
			t1=new TextField;
			t1.x=230;
			t1.y=340;
			t1.background=true;
			t1.backgroundColor=0xffffff;
			t1.text="开始";
			t1.height=30;
			t1.border=true;
			t1.selectable=false;
			addChild(t1);
			
			t2=new TextField;
			t2.background=true;
			t2.backgroundColor=0xff0000;
			t2.selectable=false;
			addChild(t2);
			t2.text="请点开始";
			
			t3=new TextField;
			t3.type=TextFieldType.INPUT;
			t3.background=true;
			t3.backgroundColor=0xffffff;
			t3.text="12";
			t3.height=20;
			t3.width=50;
			t3.border=true;
			t3.restrict="0-9";
			addChild(t3);
			
			t4=new TextField;
			t4.x=230;
			t4.y=340;
			t4.text="请输入奖品个数（最好不超过20个)：";
			t4.height=30;
			t4.selectable=false;
			addChild(t4);
			
			txtAlign();
			makeList();
			
			t1.addEventListener(MouseEvent.CLICK,onBtnClick);
			t.addEventListener(TimerEvent.TIMER,onTime);
			t3.addEventListener(Event.CHANGE,onInput);
		}
		
		private function onInput(e:Event):void
		{
			makeList();
		}
		
		private function makeList():void
		{
			if(list){
				list.removeAll();
				list=null;
			}
			
			totalGiftNum=int(t3.text);
			
			var a:Array=[];
			
			for(var i:int=0;i<totalGiftNum;i++)
			{
				var o:Object=new Object;
				a.push(o);
			}
			
			list=new List_SM(Item,3,10,4,10);
			list.dataProvider=a;
			addChild(list);
			PopUpUtils.centerInStage(list);
			list.y-=100;
		}
		
		private function txtAlign():void
		{
			t4.width=t4.textWidth;
			PopUpUtils.centerInStage(t4);
			t4.x-=30;
			t4.y+=80;
			PopUpUtils.centerInStage(t3);
			t3.y+=75;
			t3.x+=t4.width/2+10;
			
			t1.width=t1.textWidth+4;
			PopUpUtils.centerInStage(t1);
			t1.y+=160;
			
			t2.width=t2.textWidth+4;
			t2.autoSize=TextAlign.CENTER;
			PopUpUtils.centerInStage(t2);
			t2.y+=230;
		}
		
		private function onTime(e:TimerEvent):void
		{
			currentBling++;
			
			if(currentBling==totalGiftNum){
				currentBling=0;
			}
			
			if(isTickMode){
				if(nextRunGap<0){
					t.reset();
					return;
				}
				doTick();
			}else{
				doBling();
			}
		}
		
		private function doBling():void
		{
			for(var i:int=0;i<totalGiftNum;i++)
			{
				var it:Item=Item(list.getItemAt(i));
				
				if(currentBling==i){
					stopAllBling();
					it.doBling();
				}
			}
			
			if(isTickMode){
				nextRunGap--;
			}
		}
		
		private function onBtnClick(e:MouseEvent):void
		{
			if(t1.text=="开始"){
				start();
			}else{
				giftIndex=int(Math.random()*totalGiftNum);
				t2.text="你抽中的是第"+(giftIndex+1)+"格";
				t1.text="开始";
				nextRunGap=(totalGiftNum-(currentBling+1))+totalGiftNum+giftIndex+1;//点停止后总共还需要跑多少格子,从1开始
				nextRunGapNoChange=nextRunGap;
				leftTime=6000-(nextRunGapNoChange*delay);//最后跑的总格子，除了正常时间，多出多少时间
				setX();
				isTickMode=true;
				doTick();
			}
			txtAlign();
		}
		
		private function start():void
		{
			x1=0;
			t2.text="请点停止";
			isTickMode=false;
			t1.text="停止";
			t.reset();
			t.delay=delay;
			currentBling=0;
			doBling();
			t.start();
		}
		
		private function stopAllBling():void
		{
			for(var i:int=0;i<totalGiftNum;i++)
			{
				var it:Item=Item(list.getItemAt(i));
				it.recover();
			}
		}
		
		private function doTick():void
		{
			t.reset();
			t.delay=getCurrentTickTime();
			t.start();
			doBling();
		}
		
		private function setX():void
		{
			var total:int=nextRunGapNoChange;
			
			for(var i:int=0;i<nextRunGapNoChange;i++)
			{
				if(total==0){
					break;
				}
				x1+=nextRunGapNoChange-(total-1);
				total--;
			}
			x2=leftTime/x1;
		}
		
		private function getCurrentTickTime():uint
		{
			var delayAdd:uint=delay+((nextRunGapNoChange-nextRunGap)*x2);
			
			return delayAdd;
		}
	}
}